package com.efast.dmom.aps.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.aps.bean.Plant;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.entity.base.IModificationFlags;
import com.efast.dmom.aps.service.PlantService;

/**
 * 
 * @author WJ
 * @date modify 2020/2/24
 *
 */
@Controller
@RequestMapping(value="/plant")
public class PlantController {
	@Autowired
	private PlantService plantService;
	
	@RequestMapping(value = "/operateByPlant")
	public @ResponseBody ResultBean operateByPlant(@RequestBody Plant plant) {
		ResultBean resultBean = new ResultBean(true, ' ');
		
		// Determine the new
		if (plant.getInnerData().getModificationFlag() == IModificationFlags.NEW) {
			resultBean = plantService.save(plant);
			// Judge update
		} else if (plant.getInnerData().getModificationFlag() == IModificationFlags.MODIFIED) {
			resultBean = plantService.update(plant);
			// Judge to delete
		} else if (plant.getInnerData().getModificationFlag() == IModificationFlags.DELETED) {
			resultBean = plantService.delete(plant);
		}
		return resultBean;
	}

	// get plant by id
	@RequestMapping(value = "/getPlantById/{id}")
	public @ResponseBody Plant getPlantById(@PathVariable long id) {
		return plantService.getById(id);
	}

	// get plant by name
	@RequestMapping(value = "/getPlantByName/{name}")
	public @ResponseBody Plant getPlantByName(@PathVariable String name) {
		return plantService.getByName(name);
	}

	// get plant by code
	@RequestMapping(value="/getPlantByCode/{plantCode}")
	public @ResponseBody Plant getPlantByCode(@PathVariable String plantCode) {
		return plantService.getByCode(plantCode);
	}
	/**
	 * 查询所有的工厂 返回值 (工厂代码,名称,id的集合)
	 * **/
	@RequestMapping(value = "queryPlantCodeIdNameList")
	public @ResponseBody List<Map<String,String>> queryPlantCodeIdNameList() {
		return plantService.queryPlantCodeIdNameList();
	}
//	@RequestMapping(value="/getAllPlant")
//	public @ResponseBody List<Plant> getAllPlant() {
//		return plantService.getAllPlant();
//	}
	
}
